"""Password reset repository (Synchronous)."""

from __future__ import annotations

from datetime import datetime

from sqlalchemy.orm import Session

from ..models.password_reset_token import PasswordResetToken
from .base import Repository
from ..schemas.password_reset import PasswordResetCreate, PasswordResetUpdate


class PasswordResetRepository(
    Repository[PasswordResetToken, PasswordResetCreate, PasswordResetUpdate]
):
    def __init__(self, session: Session) -> None:
        super().__init__(PasswordResetToken, session)

    def get_by_token(self, token: str) -> PasswordResetToken | None:
        return (
            self.session.query(PasswordResetToken).filter(PasswordResetToken.token == token).first()
        )

    def get_valid_token(self, token: str, now: datetime) -> PasswordResetToken | None:
        return (
            self.session.query(PasswordResetToken)
            .filter(
                PasswordResetToken.token == token,
                PasswordResetToken.used.is_(False),
                PasswordResetToken.expires_at > now,
            )
            .first()
        )
